All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.JTextArea

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----com.sun.java.swing.JComponent
                           |
                           +----com.sun.java.swing.text.JTextComponent
                                   |
                                   +----com.sun.java.swing.JTextArea

public class JTextArea
extends JTextComponent
A TextArea is a multi-line area that displays plain text. It is intended to be a lightweight component that provides source compatibility with the java.awt.TextArea class where it can reasonably do so. This component has capabilities not found in the java.awt.TextArea class. The superclass should be consulted for additional capabilities. Alternative multi-line text classes with more capabilitites are JTextPane and JEditorPane.

The java.awt.TextArea internally handles scrolling. JTextArea is different in that it doesn't manage scrolling, but implements the swing Scrollable interface. This allows it to be placed inside a JScrollPane if scrolling behavior is desired, and used directly if scrolling is not desired.

The java.awt.TextArea has the ability to do line wrapping. This was controlled by the horizontal scrolling policy. Since scrolling is not done by JTextArea directly, backward compatibility must be provided another way. JTextArea has a bound property for line wrapping that controls whether or not it will wrap lines.

The java.awt.TextArea could be monitored for changes by adding a TextListener for TextEvent's. In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired. The code fragment might look something like:


    DocumentListener myListener = ??;
    JTextArea myArea = ??;
    myArea.getDocument().addDocumentListener(myListener);
 

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JTextArea key assignments.

Warning: serialized objects of this class will not be compatible with future swing releases. The current serialization support is appropriate for short term storage or RMI between Swing1.0 applications. It will not be possible to load serialized Swing1.0 objects with future releases of Swing. The JDK1.2 release of Swing will be the compatibility baseline for the serialized form of Swing objects.

See Also:
JTextPane, JEditorPane

Constructor Index

 o JTextArea()
Constructs a new TextArea.
 o JTextArea(Document)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).
 o JTextArea(Document, String, int, int)
Constructs a new JTextArea with the specified number of rows and columns, and the given model.
 o JTextArea(int, int)
Constructs a new empty TextArea with the specified number of rows and columns.
 o JTextArea(String)
Constructs a new TextArea with the specified text displayed.
 o JTextArea(String, int, int)
Constructs a new TextArea with the specified text and number of rows and columns.

Method Index

 o append(String)
Appends the given text to the end of the document.
 o createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given.
 o getAccessibleContext()
Get the AccessibleContext associated with this JTextArea.
 o getColumns()
Returns the number of columns in the TextArea.
 o getColumnWidth()
Gets column width.
 o getLineCount()
Determines the number of lines contained in the area.
 o getLineEndOffset(int)
Determines the offset of the end of the given line.
 o getLineOfOffset(int)
Translates an offset into the components text to a line number.
 o getLineStartOffset(int)
Determines the offset of the start of the given line.
 o getLineWrap()
Gets the line-wrapping policy of the text area.
 o getMinimumSize()
Returns the minimum size Dimensions of the TextArea.
 o getPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component is embedded in a JScrollPane.
 o getPreferredSize()
Returns the preferred size of the TextArea.
 o getRowHeight()
Defines the meaning of the height of a row.
 o getRows()
Returns the number of rows in the TextArea.
 o getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
 o getScrollableUnitIncrement(Rectangle, int, int)
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation.
 o getTabSize()
Gets the number of characters used to expand tabs.
 o getUIClassID()
Returns the class ID for the UI.
 o getWrapStyleWord()
Get the style of wrapping used if the text area is wrapping lines.
 o insert(String, int)
Inserts the specified text at the specified position.
 o isManagingFocus()
Turns off tab traversal once focus gained.
 o paramString()
Returns the String of parameters for this TextArea (rows and columns).
 o replaceRange(String, int, int)
Replaces text from the indicated start to end position with the new text specified.
 o setColumns(int)
Sets the number of columns for this TextArea.
 o setFont(Font)
Sets the current font.
 o setLineWrap(boolean)
Sets the line-wrapping policy of the text area.
 o setRows(int)
Sets the number of rows for this TextArea.
 o setTabSize(int)
Sets the number of characters to expand tabs to.
 o setWrapStyleWord(boolean)
Set the style of wrapping used if the text area is wrapping lines.

Constructors

 o JTextArea
 public JTextArea()
Constructs a new TextArea. A default model is set, the initial string is null, and rows/columns are set to 0.

 o JTextArea
 public JTextArea(String text)
Constructs a new TextArea with the specified text displayed. A default model is created and rows/columns are set to 0.

Parameters:
text - the text to be displayed, or null
 o JTextArea
 public JTextArea(int rows,
                  int columns)
Constructs a new empty TextArea with the specified number of rows and columns. A default model is created, and the initial string is null.

Parameters:
rows - the number of rows >= 0
columns - the number of columns >= 0
 o JTextArea
 public JTextArea(String text,
                  int rows,
                  int columns)
Constructs a new TextArea with the specified text and number of rows and columns. A default model is created.

Parameters:
text - the text to be displayed, or null
rows - the number of rows >= 0
columns - the number of columns >= 0
 o JTextArea
 public JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults for all of the other arguments (null, 0, 0).

Parameters:
doc - the model to use
 o JTextArea
 public JTextArea(Document doc,
                  String text,
                  int rows,
                  int columns)
Constructs a new JTextArea with the specified number of rows and columns, and the given model. All of the constructors feed through this constructor.

Parameters:
doc - the model to use, or create a default one if null
text - the text to be displayed, null if none
rows - the number of rows >= 0
columns - the number of columns >= 0

Methods

 o getUIClassID
 public String getUIClassID()
Returns the class ID for the UI.

Returns:
the ID ("TextAreaUI")
Overrides:
getUIClassID in class JComponent
See Also:
getUIClassID, getUI
 o createDefaultModel
 protected Document createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given. A new instance of PlainDocument is returned.

Returns:
the default document model
 o setTabSize
 public void setTabSize(int size)
Sets the number of characters to expand tabs to. This will be multiplied by the maximum advance for variable width fonts. A PropertyChange event ("TabSize") is fired when the tab size changes.

Parameters:
size - number of characters to expand to
See Also:
getTabSize
 o getTabSize
 public int getTabSize()
Gets the number of characters used to expand tabs. If the document is null or doesn't have a tab setting, return a default of 8.

Returns:
the number of characters
 o setLineWrap
 public void setLineWrap(boolean wrap)
Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("LineWrap") is fired when the policy is changed.

Parameters:
wrap - indicates if lines should be wrapped.
See Also:
getLineWrap
 o getLineWrap
 public boolean getLineWrap()
Gets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped.

Returns:
s if lines will be wrapped.
 o setWrapStyleWord
 public void setWrapStyleWord(boolean word)
Set the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundries (ie whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundries.

Parameters:
word - indicates if word boundries should be used for line wrapping.
See Also:
getWrapStyle
 o getWrapStyleWord
 public boolean getWrapStyleWord()
Get the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundries (ie whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundries.

Returns:
s if the wrap style should be word boundries instead of character boundries.
 o getLineOfOffset
 public int getLineOfOffset(int offset) throws BadLocationException
Translates an offset into the components text to a line number.

Parameters:
offset - the offset >= 0
Returns:
the line number >= 0
Throws: BadLocationException
thrown if the offset is less than zero or greater than the document length.
 o getLineCount
 public int getLineCount()
Determines the number of lines contained in the area.

Returns:
the number of lines >= 0
 o getLineStartOffset
 public int getLineStartOffset(int line) throws BadLocationException
Determines the offset of the start of the given line.

Parameters:
line - the line number to translate >= 0
Returns:
the offset >= 0
Throws: BadLocationException
thrown if the line is less than zero or greater or equal to the number of lines contained in the document (as reported by getLineCount).
 o getLineEndOffset
 public int getLineEndOffset(int line) throws BadLocationException
Determines the offset of the end of the given line.

Parameters:
line - the line >= 0
Returns:
the offset >= 0
Throws: BadLocationException
Thrown if the line is less than zero or greater or equal to the number of lines contained in the document (as reported by getLineCount).
 o insert
 public void insert(String str,
                    int pos)
Inserts the specified text at the specified position. Does nothing if the model is null or if the text is null or empty.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
str - the text to insert
pos - the position at which to insert >= 0
Throws: IllegalArgumentException
if pos is an invalid position in the model
See Also:
setText, replaceRange
 o append
 public void append(String str)
Appends the given text to the end of the document. Does nothing if the model is null or the string is null or empty.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
str - the text to insert
See Also:
insert
 o replaceRange
 public void replaceRange(String str,
                          int start,
                          int end)
Replaces text from the indicated start to end position with the new text specified. Does nothing if the model is null. Simply does a delete if the new string is null or empty.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
str - the text to use as the replacement
start - the start position >= 0
end - the end position >= start
Throws: IllegalArgumentException
if part of the range is an invalid position in the model
See Also:
insert, replaceRange
 o isManagingFocus
 public boolean isManagingFocus()
Turns off tab traversal once focus gained.

Returns:
true, to indicate that the focus is being managed
Overrides:
isManagingFocus in class JComponent
 o getRows
 public int getRows()
Returns the number of rows in the TextArea.

Returns:
the number of rows >= 0
 o setRows
 public void setRows(int rows)
Sets the number of rows for this TextArea. Calls invalidate() after setting the new value.

Parameters:
rows - the number of rows >= 0
Throws: IllegalArgumentException
if rows is less than 0
See Also:
getRows
 o getRowHeight
 protected int getRowHeight()
Defines the meaning of the height of a row. This defaults to the height of the font.

Returns:
the height >= 1
 o getColumns
 public int getColumns()
Returns the number of columns in the TextArea.

Returns:
number of columns >= 0
 o setColumns
 public void setColumns(int columns)
Sets the number of columns for this TextArea. Does an invalidate() after setting the new value.

Parameters:
columns - the number of columns >= 0
Throws: IllegalArgumentException
if columns is less than 0
See Also:
getColumns
 o getColumnWidth
 protected int getColumnWidth()
Gets column width. The meaning of what a column is can be considered a fairly weak notion for some fonts. This method is used to define the width of a column. By default this is defined to be the width of the character m for the font used. This method can be redefined to be some alternative amount.

Returns:
the column width >= 1
 o getPreferredSize
 public Dimension getPreferredSize()
Returns the preferred size of the TextArea. This is the maximum of the size needed to display the text and the size requested for the viewport.

Returns:
the size
Overrides:
getPreferredSize in class JComponent
 o getMinimumSize
 public Dimension getMinimumSize()
Returns the minimum size Dimensions of the TextArea. By default this is set to the preferred size. If columns and rows are both 0, then return super.getMinimumSize().

Returns:
the dimensions
Overrides:
getMinimumSize in class JComponent
 o setFont
 public void setFont(Font f)
Sets the current font. This removes cached row height and column width so the new font will be reflected, and calls revalidate().

Parameters:
f - the font to use as the current font
Overrides:
setFont in class Component
 o paramString
 protected String paramString()
Returns the String of parameters for this TextArea (rows and columns).

Returns:
the string of parameters
Overrides:
paramString in class Container
 o getScrollableTracksViewportWidth
 public boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport. This is implemented to return true if the line wrapping policy is true, and false if lines are not being wrapped.

Returns:
true if a viewport should force the Scrollables width to match its own.
Overrides:
getScrollableTracksViewportWidth in class JTextComponent
 o getPreferredScrollableViewportSize
 public Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component is embedded in a JScrollPane. This uses the desired column and row settings if they have been set, otherwise the superclass behavior is used.

Returns:
The preferredSize of a JViewport whose view is this Scrollable.
Overrides:
getPreferredScrollableViewportSize in class JTextComponent
See Also:
getPreferredSize
 o getScrollableUnitIncrement
 public int getScrollableUnitIncrement(Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. This is implemented to use the vaules returned by the getRowHeight and getColumnWidth methods.

Scrolling containers, like JScrollPane, will use this method each time the user requests a unit scroll.

Parameters:
visibleRect - the view area visible within the viewport
orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
direction - Less than zero to scroll up/left, greater than zero for down/right.
Returns:
The "unit" increment for scrolling in the specified direction
Throws: IllegalArgumentException
for an invalid orientation
Overrides:
getScrollableUnitIncrement in class JTextComponent
See Also:
setUnitIncrement, getRowHeight, getColumnWidth
 o getAccessibleContext
 public AccessibleContext getAccessibleContext()
Get the AccessibleContext associated with this JTextArea. Creates a new context if necessary.

Returns:
the AccessibleContext of this JTextArea
Overrides:
getAccessibleContext in class JTextComponent

All Packages  Class Hierarchy  This Package  Previous  Next  Index